<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('主体类型')" prop="subjectType">
            <dictSelect
              v-model="form.subjectType"
              :disabled="this.$route.query.id"
              :clearable="true"
              dictionary="main_body_type_all"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('主体名称')" prop="subjectId">
            <MainBodySelect
              v-model="form.subjectId"
              :disabled="this.$route.query.id"
              :parameters="form.subjectType"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('账户类型')" prop="accountType">
            <el-select v-model="form.accountType" :placeholder="$t('账户类型')">
              <el-option
                v-for="dict in accountTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('账户名称')" prop="accountName">
            <el-input v-model="form.accountName" :placeholder="$t('账户名称')" show-word-limit :maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('开户行')" prop="openingBank">
            <el-input v-model="form.openingBank" :placeholder="$t('开户行')" show-word-limit :maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('账号')" prop="accountNumber">
            <el-input v-model="form.accountNumber" :placeholder="$t('账号')" show-word-limit :maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit :maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="fund_account" :business-id="autoBusinessId($route.query.id, form)" :btn-show="!otherBtnHide" />
    </el-form>
    <div v-show="!btnHide" slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="fund:account" />
    </div>
  </div>
</template>

<script>
    import { saveFundAccount, getFundAccount } from '@/api/fund/account/fundAccount'
    import MainBodySelect from '@/components/MainBodySelect/index'
    export default {
        components: {
          MainBodySelect
        },
        data() {
            return {
                otherBtnHide: false,
                // 账户类型字典
                accountTypeOptions: [], // 表单参数
                form: {
                  fileList: []
                },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                accountType: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ], accountName: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ] },
                // 是否隐藏保存等按钮
                btnHide: false
            }
        },
        created() {
            this.getDicts('fund_account_type').then(response => {
                this.accountTypeOptions = response.data
            })
          this.$bus.$emit('async_done')
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getFundAccount(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
              this.form = {
                id: undefined,

                fundId: this.$route.query.id,

                subjectType: 'fund',

                subjectId: this.$route.query.id,

                securityAccountName: undefined,

                securityAccountNumber: undefined,

                securityOpenBroker: undefined,

                businessOutlets: undefined,

                shareholderAccount: undefined,

                tradingSeatNo: undefined,

                traderNumber: undefined,

                registrationNumber: undefined,

                traderOne: undefined,

                traderTwo: undefined,

                managerContactInformation: undefined,

                securityContactInformation: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                fileList: []
              }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面 一般户
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveFundAccount(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           }

        }

    }
</script>
